package cn.net.autocode.files.image;

import cn.net.autocode.files.ocr.OcrFactory;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class ImageTools {

    public static String parse(File file) {
        // 输出识别结果
        return ImageTools.parse(file,null);
    }

    public static String parse(File file,String language){
        try {
            if(language==null || language.isBlank()){
                language = "chi_sim";
            }
            Tesseract tesseract = OcrFactory.createTesseract(language);
            // 输出识别结果
            return tesseract.doOCR(file);
        } catch (TesseractException e) {
            return e.getMessage();
        }
    }

    public static String parse(InputStream inputStream){
        return ImageTools.parse(inputStream,null);
    }

    public static String parse(InputStream inputStream,String language){
        if(language==null || language.isBlank()){
            language = "chi_sim";
        }
        Tesseract tesseract = OcrFactory.createTesseract(language);
        try {
            BufferedImage bufferedImage = ImageIO.read(inputStream);
            return tesseract.doOCR(bufferedImage);
        } catch (IOException | TesseractException e) {
            return e.getMessage();
        }
    }


}
